
/**
 * 支付页面
 */
Ext.define("PRO.shop.view.pay.shopPayView",{
    extend: 'Ext.panel.Panel',
    border:0,
    layout:'column',
    itemId:'shopPayView',
    cls:'shopPayView',
    id:'shopPayView',
    alias: 'widget.shop.shoppayview',
    autoScroll : true,
    initComponent: function(){
        var me = this;
        me.bodyStyle="padding:10px 0;border-width:0;";
        me.getData = me.getData();
        me.needPay = JE.isEmpty(me.getData.Tteam)?me.getData.plan[0].buyMoney:me.getData.Tteam[0].buyMoney||0;
        me.year = JE.isEmpty(me.getData.Tteam)?me.getData.plan[0].buyYear:me.getData.Tteam[0].buyYear;
        me.bbar = {
            xtype:'container',
            layout:'hbox',
            itemId:'payBox',
            cls:'payBoxcls',
            items:[{
                xtype:'box',
                itemId:'money',
                flex:1,
                data:{
                    needPay :me.setNumStyle(JE.isEmpty(me.getData.Tteam)?me.getData.plan[0].buyMoney:me.getData.Tteam[0].buyMoney||0),
                    payStyle: me.getData.pay[0].payTypeName,
                    year:me.year,
                    selected:false,
                    selectedPre:me.selectedPre||10
                },
                tpl:[
                '<div style="font-size: 16px;float: left;"><div class="blanceBox">',
                //<tpl if="selected">jeicon-check-square1<tpl else>jeicon-square-o</tpl>
                    '<p class="blance"><i class="jeicon " style="margin-right:10px;color:#999;"></i><input class="Bike" type="checkbox" name="Bike" style="display:none;"/>账户余额￥'+me.getData.balance+'</p>',
                '</div>',
                '<p>'+me.name+'<span if="!me.winName">*{selectedPre}人</span>*{year}<font style="margin-left: 30px;color:999999;">需{payStyle}支付：</font>',
                '<font style="margin-left: 15px;font-size: 22px;color:#FF0000;">¥{needPay}</font></p></div>'
                ]
            },{
                xtype:'button',
                cls:'btn JEPLUS_B_L10',
                width:100,
                style:'padding: 10px 12px !important;margin-top:20px',
                text:'下一步',
                handler:function(btn){
                    //请求弹框
                    if(me.enabledLength==me.getData.plan.length&&me.winName==false){
                        JE.alert('超出购买范围，请联系客服400-0999235')
                    }else{
                        me.setNext(btn)
                    }
                }
            }],
            listeners:{
                afterRender:function(view){
                    //处理支付方式返回值
                    if(me.getData.pay[0].payTypeCode=='10'||me.beforeSel.payTypeCode=='10'){
                        var blance = document.querySelector('.blance');
                        var blanceInput = blance.querySelector('input');
                        blance.style.display="none";
                    }
                    if(me.beforeSel&&JE.isNotEmpty(me.beforeSel)){
                        var moneyPanel = me.getCmp('payBox').getCmp('money');
                        var blance = document.querySelector('.blance');
                        me.payFont = me.beforeSel.payTypeName;
                        me.needPay = me.beforeSel.needPay;
                        me.year = me.beforeSel.year;
                        moneyPanel.update({
                            needPay : me.beforeSel.needPay,
                            payStyle: me.beforeSel.payTypeName,
                            year:me.beforeSel.year,
                            selected:false,//me.beforeSel.selected
                            selectedPre:me.selectedPre||me.getCmp('topPanel').getCmp('containerview').getCmp('number').getValue()||10
                        });
                        var boolean = me.beforeSel.selected&&me.beforeSel.payTypeCode!='10';
                        me.isShowRadio(!boolean);
                    }else{
                        // me.isShowRadio(true);
                    }
                    view.getEl().on('click',function(e){
                        if(e.getTarget('.blance',3)){
                            //选择框选中
                            var blanceBox =e.getTarget('.blance',3);
                            var blanceInput = blanceBox.querySelector('input');
                            var moneyPanel = me.getCmp('payBox').getCmp('money');
                            if(blanceInput.checked){
                                //否
                                //微信或者支付宝需要支付全额
                                //下一步的传参需要直接扫描二维码
                                moneyPanel.update({
                                    needPay : Number(me.needPay||JE.isEmpty(me.getData.Tteam)?me.needPay||JE.isEmpty(me.getData.Tteam):me.getData.Tteam[0].buyMoney),//me.getData().plan[0].buyMoney||0,
                                    payStyle: me.payFont,
                                    year:me.year||JE.isEmpty(me.getData.Tteam)?me.year||JE.isEmpty(me.getData.Tteam):me.getData.Tteam[0].buyYear,
                                    selected:false
                                });
                                me.isShowRadio();
                                // me.isShowRadio(true);
                            }else{

                                //是
                                //计算微信或者支付宝需要支付多少
                                moneyPanel.update({
                                    needPay : me.getNeedPay(),//me.getData().plan[0].buyMoney||0,
                                    payStyle: me.payFont,
                                    year:me.year||JE.isEmpty(me.getData.Tteam)?me.year||JE.isEmpty(me.getData.Tteam):me.getData.Tteam[0].buyYear,
                                    selected:false//true
                                });
                               me.isShowRadio();
                            }
                        }
                    })
                }
            }
        };
        var tpl =[
            '<div>',
                '<div>',
                    '<p class="buyTit">购买方案',
                        '<tpl if="!winName">',
                        '<i class="shopQuestion jeicon jeicon-question-circle"></i>',
                        '<span class="shopQusetIntro">系统默认赠送15位免费名额，新购套餐将按最新人员总数计费。</span>',
                        '</tpl>',
                    '</p>',
                    '<div class="buyRightnow">',
                        '<tpl for="plan">',
                            '<tpl if="!enabled">',
                                '<div class="planBuy planBuyflex <tpl if="index==1">',
                                                                   '<tpl if="dis">activePlanbuyorg<tpl else> activePlanbuy</tpl>',
                                                                '</tpl>" data-dis="{dis}"  data-year="{buyYear}" data-otherpay={buyMoney} data-schemeid={schemeId}>',
                                        '<tpl if="dis"><img class="discount" src="'+JE.buildCdnUrl('/pro/shop/resourse/discount.png')+'"></tpl>',
                                        '<p class="planBuyP" >',
                                            '<font class="buyMoney">{buyYear}</font>',
                                            '<font class="buyYear">¥{buyMoney}</font>',
                                        '</p>',
                                        '<tpl if="dis"><span class="discountBg">{discount}</span></tpl>',
                                '</div>',
                                '<tpl else>',
                                '<div class="planBuyflexNot" data-dis="{dis}"  data-year="{buyYear}" data-otherpay={buyMoney} data-schemeid={schemeId}>',
                                        '<tpl if="dis"><img class="discount" src="'+JE.buildCdnUrl('/pro/shop/resourse/discount.png')+'"></tpl>',
                                        '<p class="planBuyP" >',
                                            '<font class="buyMoney">{buyYear}</font>',
                                            '<font class="buyYear">¥{buyMoney}</font>',
                                        '</p>',
                                        '<tpl if="dis"><span class="discountBg">{discount}</span></tpl>',
                                '</div>',
                            '</tpl>',
                        '</tpl>',
                    '</div>',
                '</div>',
                '<div>',
                    '<tpl if="cptype">',
                        '<p class="buyTit">使用名额</p>',
                        '<p style="font-size:12px;color:#B3B3B3; margin-top:-10px;"><span style="font-size:16px;color:#111;">{personNum}人</span>（购买本产品默认赠送15名免费使用额，如需扩充使用人数，需单独购买产品“使用名额”）</p>',
                    '</tpl>',
                    '</div>',
                '<div class="payPlanbox">',
                    '<div class="payPlan">',
                        '<tpl for="pay">',
                        '<div class="payPlanstyle payPlanpurse" data-paytypename="{payTypeName}" data-paytypecode="{payTypeCode}" >',
                                '<i class="selectedIcon jeicon ',
                                        '<tpl if="index">jeicon-check-circle<tpl else>jeicon-circle-o</tpl> ',
                                    '"></i>',
                                '<i class="payIcon jeicon ',
                                    '<tpl if="payTypeCode==10">jeicon-purse</tpl>',
                                    '<tpl if="payTypeCode==20">jeicon-wechat</tpl>',
                                    '<tpl if="payTypeCode==30">jeicon-alipay</tpl>">',
                                '</i>',
                                '<font>{payTypeName}</font>',
                        '</div>',
                        '</tpl>',
                    '</div>',
                '</div>',
            '</div>',
            '<p class="payTit">如需定制购买方案可联系客服 <a href="javascript:;">400-0999-235</a></p>',
        ];
        me.defaults = {columnWidth:1};
        me.items=[
            {
                xtype:'box',
                cls:'playBuybox',
                html:'<font class="saleSuanban">'+me.name+'</font>'
            },{
            xtype:'panel',
            layout:'vbox',
            border:0,
            itemId:'topPanel',
            hidden:me.winName,
            defaults:{
                width:'100%'
            },
            items:[{
                xtype:'box',
                height:30,
                margin:'10 0 0 0',
                data:{},
                tpl:[
                    '<div style="color:#B3B3B3;font-size:14px;">'+me.name+'(当前总使用人数<span style="color:#2F5DBF;">'+me.getData.peekValue+'</span>人)</div>'
                ]
            },{
                xtype:'container',
                height: 40,
                margin:'0 20 10 0',
                layout: {
                    type:'hbox',
                    pack:'end'
                },
                itemId:'containerview',
                items:[{
                    xtype:'box',
                    margin:'7 10 0 0',
                    html:'<i class="jeicon jeicon-minus-line" style="color:#DDDDDD;margin-left:5px;cursor:pointer;"></i>',
                    listeners:{
                        afterRender:function(view){
                            view.getEl().on('click',function(e){
                                if(e.getTarget('.jeicon-minus-line',1)){
                                    var topPanel=me.getCmp('topPanel');
                                    var containerview=topPanel.getCmp('containerview');
                                    var number=containerview.getCmp('number')
                                    var sliderfieldview=topPanel.getCmp('sliderfieldview');
                                    var val=number.getValue();
                                    var newVal=val-10;
                                    if(newVal<10)return;
                                    sliderfieldview.setValue(newVal);
                                    number.setValue(newVal);
                                    //此处处理更改函数；
                                    me.selectedSchemeId = '';
                                    me.changeLab(newVal,true)
                                }
                             });

                        }
                    }
                },{
                    xtype:'numberfield',
                    itemId:'number',
                    value:me.selectedPre||10,
                    width:50,
                    hideTrigger:true,
                    minValue:10,
                    maxValue:me.getData.maxNumbers,
                    step:10,
                    listeners:{
                        change:function(field,newVal,oldVal){
                            var topPanel=me.getCmp('topPanel');
                            var sliderfieldview=topPanel.getCmp('sliderfieldview');
                            sliderfieldview.setValue(newVal);
                        }
                    }
                },{
                    xtype:'box',
                    margin:'7 0 0 10',
                    html:'<i class="jeicon jeicon-plus" style="color:#DDDDDD;margin-right:5px;cursor:pointer;"></i>',
                    listeners:{
                        afterRender:function(view){
                            view.getEl().on('click',function(e){
                                if(e.getTarget('.jeicon-plus',1)){
                                    var topPanel=me.getCmp('topPanel');
                                    var containerview=topPanel.getCmp('containerview');
                                    var number=containerview.getCmp('number')
                                    var sliderfieldview=topPanel.getCmp('sliderfieldview');
                                    var val=number.getValue();
                                    var newVal=val+10;
                                    if(newVal>me.getData.maxNumbers){
                                        return JE.alert('超过最大人数');
                                    }
                                    sliderfieldview.setValue(newVal);
                                    number.setValue(newVal);
                                    //此处处理更改函数；
                                    me.selectedSchemeId = '';
                                    me.changeLab(newVal,true)
                                }
                             });

                        }
                    }
                }]
            },{
                xtype:'sliderfield',
                itemId:'sliderfieldview',
                cls:'je-pf-field-slider',//je-field-slider
                margin:'0 10 40 15',
                minValue:10,
                increment:10,
                height:10,
                maxValue:me.getData.maxNumbers,
                useTips: false,
                autoShow:false,
                listeners:{
                    change:function(slider,newValue,thumb,eOpts){
                        var topPanel=me.getCmp('topPanel');
                        var containerview=topPanel.getCmp('containerview');
                        var number=containerview.getCmp('number');
                        if(newValue<10){
                            newValue = 10;
                        }
                        number.setValue(newValue);
                        var pfcase=slider.getEl().down('.pfcase');
                        slider.tipTextEl.el.dom.innerText=newValue;
                        var sliderthumb=slider.getEl().down('.x-slider-thumb');
                        var sliderthumbStyle=(newValue/me.getData.maxNumbers)*100+"%";
                        var pfslider=slider.getEl().down('.pfslider');
                        var sliderthumbStyletip=((newValue/me.getData.maxNumbers)*100)+"%";
                        slider.tipEl.setStyle({width:sliderthumbStyletip})
                        me.selectedSchemeId = '';
                        me.changeLab(newValue,true)
                    },
                    afterrender:function(field,eOpts){
                        var sliderinner=this.getEl().down('.x-slider-inner');
                        var xslider=this.getEl().down('.x-slider');
                        var sliderthumb=this.getEl().down('.x-slider-thumb');
                        var pfcaseText = me.selectedPre||10;
                        sliderthumb.setStyle({'z-index':10000,'left':((pfcaseText/me.getData.maxNumbers)*100)+'%'})
                        var horz=this.getEl().down('.x-slider-horz');
                        horz.setStyle({'pointer-events':'all'});
                        field.tipEl =sliderinner.insertHtml('beforeEnd','<div class="pfcase"><div class="pfslider"></div><div class="tip-text pfcasedown">'+pfcaseText+'</div></div>',true)
                        field.tipTextEl = field.tipEl.down('.tip-text');
                        field.tipEl.alignTo(field.thumbs[0].el, 'bc', [0,30]);
                        field.pfsliderEl=this.getEl().down('.pfslider');
                        var pfcase = this.getEl().down('.pfcase');
                        pfcase.setStyle({left:0,top:0,width:((pfcaseText/me.getData.maxNumbers)*100)+'%'});
                    }
                }
            }]
        },{
            xtype:'box',
            itemId:'centerPanel',
            data:me.getData,
            tpl:tpl
        }]
        me.callParent(arguments);
    },
    afterRender:function(){
        var me = this;
        me.callParent(arguments);
        var moneyPanel = me.getCmp('payBox').getCmp('money');
        me.selectedPre?me.changeLab(me.selectedPre):'';
        me.body.on('click',function(e){
            if(e.getTarget('.downBtn',1)){
                //处理页面
                me.up('window').close();
            }
            if(e.getTarget('.planBuy',3)){
                //购买方案
                var planBuy = e.getTarget('.planBuy',3);
                var needPay = planBuy.getAttribute('data-otherpay');
                var schemeId = planBuy.getAttribute('data-schemeid');
                var dis = planBuy.getAttribute('data-dis');
                var year = planBuy.getAttribute('data-year');
                var otherPlan = document.getElementsByClassName('planBuy');
                me.schemeId = schemeId;
                for(var i= 0 ;i<otherPlan.length;i++){
                    otherPlan[i].setAttribute('class','planBuy planBuyflex')
                }
                if(dis=='true'){
                    planBuy.setAttribute('class','planBuy planBuyflex activePlanbuyorg')
                }else{
                    planBuy.setAttribute('class','planBuy planBuyflex activePlanbuy')
                }
                me.needPay = needPay;
                me.year = year;
                moneyPanel.update({
                    payStyle:me.payFont||'账户余额',
                    needPay:me.setNumStyle(needPay),
                    year:year,
                    selectedPre:me.selectedPre||me.getCmp('topPanel').getCmp('containerview').getCmp('number').getValue()||10
                });
                me.isShowRadio();
            }
            //支付方式
            if(e.getTarget('.payPlanstyle',3)){
                var payPlanstyle = e.getTarget('.payPlanstyle',3);
                var payFont = payPlanstyle.getAttribute('data-paytypename');
                var blance = document.querySelector('.blance');
                var blanceInput = blance.querySelector('input');
                blanceInput.removeAttribute('checked');
                me.payFont = payFont;
                moneyPanel.update({
                    payStyle:payFont,
                    needPay:me.setNumStyle(me.needPay||JE.isEmpty(me.getData.Tteam)?me.needPay||JE.isEmpty(me.getData.Tteam):me.getData.plan[0].buyMoney),
                    year:me.year||JE.isEmpty(me.getData.Tteam)?me.year||JE.isEmpty(me.getData.Tteam):me.getData.plan[0].buyYear,
                    selectedPre:me.selectedPre||me.getCmp('topPanel').getCmp('containerview').getCmp('number').getValue()||10
                });
                me.isShowRadio(true);
                var payBuy = payPlanstyle.querySelector('.selectedIcon');
                var otherPay= document.getElementsByClassName('selectedIcon');
                for(var i= 0 ;i<otherPay.length;i++){
                    otherPay[i].setAttribute('class','selectedIcon jeicon jeicon-circle-o');
                }
                me.useBalance?me.useBalance=0:'';
                payBuy.setAttribute('class','selectedIcon jeicon jeicon-check-circle');
            }
        });
    },
    getData:function(){
        var me = this;
        var resData = me.resData.obj;
        var shopName = resData.productInfo.values.CP_MC;
        var plan = [],pay = [],Tteam=[];
        me.enabledLength = 0;
        Ext.each(resData.schemeInfo,function(planItem,index){
            if(planItem.values.enabled=='T'){
                Tteam.push({
                    text:index+1,
                    dis:!JE.isEmpty(me.setDis(planItem.values.coefficient)),
                    discount:(me.setDis(planItem.values.coefficient))*10+'折',
                    buyYear:planItem.values.timeLength+planItem.values.timeUnitName,
                    buyMoney:me.setNumStyle(planItem.values.schemePrice),
                    index:Tteam.length+1,
                    schemeId:planItem.values.schemeId,
                    enabled:planItem.values.enabled=='F'?true:false,
                });
            }else{
                me.enabledLength++;
            }
            plan.push({
                text:index+1,
                dis:!JE.isEmpty(me.setDis(planItem.values.coefficient)),
                discount:(me.setDis(planItem.values.coefficient))*10+'折',
                buyYear:planItem.values.timeLength+planItem.values.timeUnitName,
                buyMoney:me.setNumStyle(planItem.values.schemePrice),
                index:me.setIndex(planItem,Tteam),
                schemeId:planItem.values.schemeId,
                enabled:planItem.values.enabled=='F'?true:false
            });
        })

        Ext.each(resData.payTypesInfo,function(payItem,index){
            pay.push({
                payTypeCode:payItem.values.payTypeCode,
                payTypeName:payItem.values.payTypeName,
                index:me.beforeSel?me.getPayStyle(payItem.values.payTypeCode):(index==0?true:false)
            })

        });
        if(JE.isEmpty(Tteam)&&JE.isEmpty(me.selectedSchemeId)){
            Ext.each(plan,function(item,index){
                item.index = index+1;
            })
        }
        if(JE.isNotEmpty(me.selectedSchemeId)){
            me.schemeId = me.selectedSchemeId
        }
        var data ={
            shopName:shopName,//'蒜瓣CRM',
            id:resData.productInfo.pkValue,
            balance:JE.isEmpty(resData.accountInfo)?'0':resData.accountInfo.values.ACCOUNT_BALANCE,//余额
            personNum:resData.peekValue,//人员数量
            plan:plan,
            winName:me.winName,
            pay:pay,
            Tteam:Tteam,
            cptype:resData.productInfo.values.CP_CPTYPE_CODE=="MENUP"?false:true,
            refundAmount:resData.refundAmount||'',
            lastPurchaseProductSku:resData.lastPurchaseProductSku,
            maxNumbers:Number(resData.maxNumbers)-Number(resData.peekValue),
            peekValue:resData.peekValue,
        }
        return data;
    },
    setDis:function(charge){
        if(Number(charge)==0||Number(charge)==1){
            var dis = '';
        }else{
            var dis = Number(charge);
        }
        return dis
    },
    /*
    * 计算在勾选账户余额支付后需要支付的价格
    */
    getNeedPay:function(){
        var me = this;
        var getData = me.getData;
        var needPay;
        var price = (Number(getData.balance)*100-Number(me.needPay||JE.isEmpty(getData.Tteam)?me.needPay||JE.isEmpty(getData.Tteam):getData.Tteam[0].buyMoney)*100)*0.01;
        var needPayBoolean = price>=0?true:false;
        switch(needPayBoolean){
            case true:
            needPay = 0
            break;
            case false:
            needPay = me.formatNum(Math.abs(price),1)
            break;
        }
        return needPay
    },
    isShowRadio:function(boolean){
        var me = this;
        var blance = document.querySelector('.blance');
        var blanceInput = blance.querySelector('input');
        if(me.payFont=='微信'||me.payFont=='支付宝'){
            // blance.style.display="block";
            blance.style.display="none";
        }else{
            //现金支付 隐藏现金条
            blance.style.display="none";
        }
        if(boolean){
            blanceInput.removeAttribute('checked');
        }else{
            blanceInput.removeAttribute('checked');
            // blanceInput.setAttribute('checked',true);
        }
    },
    /*
    * 下一步去哪里
    */
    setNext:function(btn){
        //去支付页面
        var me = this;
        var _btn = btn;
        var saveOrder =  me.saveOrder();
        if(saveOrder.success){
            var orderId = saveOrder.obj;
        }else{
            if(saveOrder.code=='202'||saveOrder.code=='204'){
                return JE.alert('该订单已重复下单，请前往“订单管理”完成支付或取消订单')
            }else{
                return JE.alert(saveOrder.message)
            }

        }
        /**
         * 1.账户余额勾选---跳转账户余额
         * 2.账户余额未勾选---非账户余额跳转
        */
        var blance = document.querySelector('.blance');
        var blanceInput = blance.querySelector('input');
        var useBalace = blanceInput.getAttribute('checked');
        if(me.getData.refundAmount&&me.getData.refundAmount!='0'){
            var html  = '<div>您正在使用的套餐【'+me.getData.lastPurchaseProductSku+'】尚未到期，若新购本套餐，'+
                            '则已购套餐未使用费用<span style="color:red;">'+me.getData.refundAmount+'元</span>将用于本次购买费用抵扣，购买成功，'+
                            '则新套餐立即生效，请及时至员工管理中重新设置员工“有效”状态。'
                        '</div>'
            JE.confirm(html,function(btn){
                if(btn=='ok'){
                    // var accoutMoney = me.getAccount();
                    //选择了账户余额支付，账户余额为0；
                    if(useBalace){
                    // if(useBalace&&accoutMoney){
                        me.goQRcode(orderId)
                    }else if(me.payFont!='账户余额'&&me.payFont){
                        me.payTheOrder(orderId,'',_btn);
                    }else{
                        me.goQRcode(orderId)
                    }
                }else{
                    _btn.up('window').close();
                }
              })
        }else{
            // var accoutMoney = me.getAccount();
             //选择了账户余额支付，账户余额为0；
            if(useBalace){
                me.goQRcode(orderId)
                // me.payTheOrder(orderId,'',_btn);
            }else if(me.payFont!='账户余额'&&me.payFont){
                me.payTheOrder(orderId,'',_btn);
            }else{
                me.goQRcode(orderId)
            }

        }


    },
    saveOrder:function(){
        var me = this;
        var blance = document.querySelector('.blance');
        var blanceInput = blance.querySelector('input');
        var useBalace = blanceInput.getAttribute('checked');
        var parth ;
        var getData = me.getData;
        var limitNumbersInput = me.getCmp('topPanel').getCmp('containerview').getCmp('number').getValue();
        var params = {
            schemeId:me.schemeId||(JE.isEmpty(getData.Tteam)?getData.plan[0].schemeId:getData.Tteam[0].schemeId),
            payTypeCode:me.getPayCode(),
            payTypeName:me.payFont||getData.pay[0].payTypeName,//me.payFont||JE.isEmpty(getData.Tteam)?'':getData.pay[0].payTypeName,
            limitNumbersInput:me.winName?'':limitNumbersInput,//人员数量
            invoiceNeed :'',//发票
            useBalance:me.payFont!='账户余额'?useBalace||me.useBalance?'1':'0':''// 是否优先使用余额支付 0代表不优先使用余额支付，1代表优先使用余额支付，如果支付类型为账户余额支付，该值可不传
        };
        if(me.type!='save'){
            params['orderId'] = me.saveOrderId;
            var parth = '/je/shoppingmall/bill/updateOrderInfo'
        }else if(JE.isNotEmpty(me.saveOrderId)){
            params['orderId'] = me.saveOrderId;
            var parth = '/je/shoppingmall/bill/updateOrderInfo'
        }else{
            params['productId'] = getData.id;
            var parth = '/je/shoppingmall/bill/saveOrder'
        }


        var obj = JE.ajax({
            url:parth,
            params:params
        })
        me.saveOrderId = obj.obj;
        return obj;

    },
    getPayCode:function(){
        var me = this;
        var getData = me.getData;
        var code = getData.pay[0].payTypeCode;
        switch (me.payFont){
            case getData.pay[0].payTypeName:
            code = getData.pay[0].payTypeCode;
            break;
            case getData.pay[1].payTypeName:
            code = getData.pay[1].payTypeCode;
            break;
            case getData.pay[2].payTypeName:
            code = getData.pay[2].payTypeCode;
            break;
        }
        return code
    },
    payTheOrder:function(orderId,validateCode,btn,win){
        var me = this;
        JE.ajax({
            url:'/je/shoppingmall/bill/payTheOrder',
            params:{
                orderId:orderId,
                validateCode:validateCode
            },
            success:function(res){
                var res = JSON.parse(res.responseText);
                if(res.success){
                    win?win.up('window').close():'';
                    var bdorderId = res.obj.orderId;
                    switch(res.obj.payResult){
                        case '1000':
                         JE.win({
                            width:290,
                            height:330,
                            border:0,
                            bodyPadding:'20px,80px',
                            default:{border:0},
                            closable: false,
                            maximizable :false,
                            items:[{xtype:'shop.payhintview',info:{text:'账户余额支付成功',class:"jeicon-purse"}}]
                        }).show();
                        var card = Ext.getCmp('shopMainEnterPanel').getCmp('cardPanel');
                        card.winType = '';
                        break;
                        //当payResult为1000时代表账户余额支付成功
                        case '1001':
                        //为1001代表用户使用支付宝或者微信支付时，选择了优先使用余额支付，
                        //并且账户内的余额完全可以支付该笔订单，无需支付宝或者微信补足剩余金额。那么1001的意思就是让你直接跳转到余额支付的页面
                        me.goQRcode(res.obj.orderId);
                        break;
                        case '1002'://支付宝
                        var resHtml = res.obj.unifiedOrderRequestHtml;
                        me.getEl().insertHtml('afterBegin',resHtml);
                        me.getAlipay(btn,bdorderId)
                        break;
                        case '1003'://微信
                        var qrCodePathSrc = "http://crm-private.oss-cn-beijing.aliyuncs.com"+res.obj.qrCodePath;
                        var money = res.obj.paymentAmount;
                        me.getWechatPay(qrCodePathSrc,money,bdorderId)
                        break;
                        case '1005'://微信
                        me.payTheOrder(orderId,validateCode,btn)
                        break;
                        case '1006'://支付宝
                        me.payTheOrder(orderId,validateCode,btn)
                        break;
                    }
                }else{
                    JE.alert(res.message)
                }
            }
        })
    },
    goQRcode:function(orderId){
        var me = this;
        JE.win({
            width:484,//JE.getBodyWidth()-750,
            height:536,//JE.getBodyHeight()-235,
            border:0,
            bodyPadding:'20px 50px',
            default:{border:0},
            items:[{
                xtype:'shop.paystyleview',
                payFont:'账户余额',
                resData:me.resData,
                winName:me.winName,
                name:me.name,
                _thisMe:me,
                orderId:orderId
            }]
        }).show();
    },
    changeLab:function(newVal,changeUse){
        var me = this;
        JE.ajax({
            url:'/je/shoppingmall/bill/getPersonPriceStragegy',
            params:{
                productId:me.resData.obj.productInfo.pkValue,
                limitNumbersInput:newVal//+me.getData.peekValue
            },
            success:function(res){
                var res = JSON.parse(res.responseText);

                if(res.success){
                    var plan = [],Tteam=[];
                    var datas = me.getData;
                    me.enabledLength=0
                    Ext.each(res.obj.schemeInfo,function(planItem,index){
                        if(planItem.values.enabled=='T'){
                            Tteam.push({
                                text:index+1,
                                dis:!JE.isEmpty(me.setDis(planItem.values.coefficient)),
                                discount:(me.setDis(planItem.values.coefficient))*10+'折',
                                buyYear:planItem.values.timeLength+planItem.values.timeUnitName,
                                buyMoney:me.setNumStyle(planItem.values.schemePrice),
                                index:Tteam.length+1,
                                schemeId:planItem.values.schemeId,
                                enabled:planItem.values.enabled=='F'?true:false,
                            });
                        }else{
                            me.enabledLength++;
                        }
                        plan.push({
                            text:index+1,
                            dis:!JE.isEmpty(me.setDis(planItem.values.coefficient)),
                            discount:(me.setDis(planItem.values.coefficient))*10+'折',
                            buyYear:planItem.values.timeLength+planItem.values.timeUnitName,
                            buyMoney:me.setNumStyle(planItem.values.schemePrice),
                            index:me.setIndex(planItem,Tteam),
                            schemeId:planItem.values.schemeId,
                            enabled:planItem.values.enabled=='F'?true:false
                        });

                    })
                };
                if(changeUse){
                    Ext.each(plan,function(item,index){
                        item.index = index+1;
                    })
                    me.schemeId = plan[0].schemeId;
                    Ext.each(datas.pay,function(item,index){
                        if(index==0){
                            item.index  = true;
                        }else{
                            item.index  = false;
                        }
                    })
                }
                datas.Tteam = Tteam;
                datas.plan = plan;
                if(JE.isNotEmpty(me.selectedSchemeId)){
                    Ext.each(plan,function(item){
                        if(item.schemeId==me.selectedSchemeId){
                            var needPay = item.buyMoney;
                            me.needPay = item.buyMoney
                        }
                    })
                }else{
                    var needPay = JE.isEmpty(datas.Tteam)?datas.plan[0].buyMoney:datas.Tteam[0].buyMoney||0;
                    me.needPay = needPay;
                }
                var year = me.year||(JE.isEmpty(datas.Tteam)?datas.plan[0].buyYear:datas.Tteam[0].buyYear);
                me.year = year;
                me.getCmp('payBox').getCmp('money').update({
                    needPay : changeUse?needPay:(me.beforeSel?me.beforeSel.needPay:needPay),
                    payStyle:changeUse?datas.pay[0].payTypeName:( me.beforeSel?me.beforeSel.payTypeName:datas.pay[0].payTypeName),
                    year:changeUse?year:(me.beforeSel?me.beforeSel.year:year),
                    selected:false,//changeUse?false:(me.useBalance?true:false),
                    selectedPre:me.selectedPre||me.getCmp('topPanel').getCmp('containerview').getCmp('number').getValue()||10
                });
                me.getCmp('centerPanel').update(datas);
                changeUse?me.payFont='账户余额':(me.beforeSel?me.payFont = me.beforeSel.payTypeName:me.payFont='账户余额');
                me.isShowRadio(changeUse?true:(me.useBalance?false:true));
            }
        })
    },
    getAlipay:function(btn,bdorderId){
        var form = Ext.get('alipaysubmit').dom;
            form.target = '_blank';
            form.submit();
        var shadowBox = btn.up('[itemId=shopPayView]')?btn.up('[itemId=shopPayView]'):btn;
        var myMask = new Ext.LoadMask(shadowBox, {msg:"正在支付，请稍后..."});
        shadowBox?myMask.show():'';
        window.PAY_SUCCESS_RES_PAGE = function(resData){
            var context = JSON.parse(resData).context;
            var resorderId = JSON.parse(context).orderId;
            if(resorderId==bdorderId){
                shadowBox?myMask.hide():'';
                var card = Ext.getCmp('shopMainEnterPanel').getCmp('cardPanel');
                card.winType = '';
                JE.win({
                    width:290,
                    height:330,
                    border:0,
                    bodyPadding:'20px,80px',
                    default:{border:0},
                    closable: false,
                    maximizable :false,
                    items:[{xtype:'shop.payhintview',info:{text:'支付宝支付成功',class:"jeicon-alipay"}}]
                }).show();
            }
        }
    },
    getWechatPay:function(qrCodePathSrc,money,bdorderId){
        var me = this;
        var wechat = JE.win({
            width:350,
            height:365,
            border:0,
            default:{border:0},
            items:[{xtype:'shop.payinfoview',info:{icon:'jeicon jeicon-weixin',text:'微信扫码支付',money:money,img:'<img style="display:block;width:100%;" src='+qrCodePathSrc+'>'}}]
        });
        wechat.show();
        window.PAY_SUCCESS_RES_PAGE = function(resData){
            var context = JSON.parse(resData).context;
            var resorderId = JSON.parse(context).orderId;
            if(resorderId==bdorderId){
                wechat.hide();
                me.up('window')?me.up('window').close():'';
                var card = Ext.getCmp('shopMainEnterPanel').getCmp('cardPanel');
                card.winType = '';
                JE.win({
                    width:290,
                    height:330,
                    border:0,
                    maximizable :false,
                    bodyPadding:'20px,80px',
                    default:{border:0},
                    closable: false,
                    maximizable :false,
                    items:[{xtype:'shop.payhintview',info:{text:'微信支付成功',class:"jeicon-wechatpay"}}]
                }).show();
            }
        }
    },
    setIndex:function(planItem,Tteam){
        /**
         * 设置默认选择方案 planItem.values.schemeId   planItem.values.enabled=='T'&&Tteam.length==1?1:''
         * 有 me.selectedSchemeId
        */
       var me = this;
       if(me.selectedSchemeId==planItem.values.schemeId){
           return 1;
       }
       if(me.selectedSchemeId){
            return ''
       }else{
            if(planItem.values.enabled=='T'&&Tteam.length==1){
                return 1
            }else{
                return ''
            }
       }
    },
    getPayStyle:function(payTypeCode){
        var me = this;
        if(payTypeCode==me.beforeSel.payTypeCode){
            return true
        }else{
            return false
        }
    },
    formatNum:function(f,digit){
        var m = Math.pow(100, digit);
        return parseInt(f * m, 10) / m;
    },
    setNumStyle:function(num){
        num = num.toString();
        if(num.includes('.')){
           if(num.substr(num.indexOf('.')+1).length==2){
               return num
           }else{
           return num = num+'0';
           }
        }else{
           return num= num+'.00';
        }
    }
});
